<template>
  <div>
    <div>
      <FormPanel ref="formPanel" :list="formList" :cols="1" :labelWidth="110" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle" />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 10,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">关 闭</el-button>
      <AjaxButton v-if="type !== 'show'" :loading="exportLoading" type="primary" :click="saveDialog">{{ btnText }}</AjaxButton>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { addWorkerGroups, editWorkerGroups } from '@/api/repair/repairBusiness/instituteDefined';
import { getWorkerGroups, getOrgList, deleteWorkgroup } from '@/api/repair/repairBusiness/instituteDefined';
import { arrayObjDeWeight } from '@/utils/index';
export default {
  name: 'editWorkerGroup',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '保 存' };
    this.saveData = null;
    return {
      exportLoading: false, // 保存载入中
      formList: this.createFormList()
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  mounted() {
    // this.getOrgList();
    this.init();
    this.JudgementState();
  },
  methods: {
    JudgementState() {
      if (this.type == 'edit') {
        this.formList.find(x => x.fieldName === 'workgroupCode').disabled = true;
      }
    },
    findIndex(arr, fieldName) {
      // console.log(arr);
      let indexR = -1;
      arr.forEach((x, index) => {
        if (x.fieldName == fieldName) {
          // console.log(x);
          // console.log(index);
          indexR = index;
        }
      });
      return indexR;
    },
    async getOrgList() {
      let res = await getOrgList({ orgType: 15061004, type: 1506, companyId: '', appId: 'cyx' });
      this.formList.find(x => x.fieldName === 'orgNo').itemList = res.data.map(x => ({ text: x.orgName, value: +x.orgId }));
    },
    init() {
      if (this.type === 'edit') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.workgroupId;
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
        this.formList.find(x => x.fieldName === 'workerTypeCode').rules = [{ required: true, message: '请选择工种', trigger: 'change' }];
        this.formList = [...this.formList];
      }
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '班组代码',
          fieldName: 'workgroupCode',
          disabled: false,
          placeholder: '请输入...',
          maxlength: 4,
          rules: [
            { required: true, message: '请输入班组代码', trigger: 'blur' },
            { min: 4, max: 4, pattern: /^[0-9a-zA-Z]{4,4}$/, message: '请输入字母和数字（四位）', trigger: 'blur' }
          ]
        },
        {
          type: 'INPUT',
          label: '班组名称',
          fieldName: 'workgroupName',
          placeholder: '请输入...',
          maxlength: 20,
          rules: [
            { required: true, message: '请输入班组名称', trigger: 'blur' },
            { min: 1, max: 60, message: '最多输入60个字符!', trigger: 'blur' }
          ]
        },
        // {
        //   type: 'SELECT',
        //   label: '部门',
        //   fieldName: 'orgNo',
        //   placeholder: '请选择...',
        //   itemList: []
        //   // rules: [{ required: true, message: '请输入部门', trigger: 'blur' }]
        // },
        {
          type: 'SELECT',
          label: '工位类别',
          fieldName: 'workerTypeCode',
          placeholder: '请选择...',
          itemList: this.createDictList('8041'),
          initialValue: 80411001
        },
        {
          type: 'SELECT',
          label: '班组状态',
          fieldName: 'isValid',
          placeholder: '请选择...',
          style: { width: '150' },
          itemList: [
            { value: 10041001, text: '启用' },
            { value: 10041002, text: '停用' }
          ]
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        let res = {};
        if (this.type === 'add') {
          res = await addWorkerGroups(this.saveData);
        }
        if (this.type === 'edit') {
          res = await editWorkerGroups(this.saveData, this.recordId);
        }
        if (res.resultCode === 200) {
          this.exportLoading = true;
          this.$notify.success({ title: '提示信息', message: '保存成功' });
          this.closeDialog('refresh');
          this.exportLoading = false;
        }
        // else {
        //   console.log(11111111111111)
        //   return this.$notify.error({ title: '提示信息', message: res.errMsg || '保存失败!' });
        // }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    }
  }
};
</script>

<style lang="less" scoped></style>
